package cn.tedu._03vehicle.controller;


import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.dto.VehicleParam;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/vehicle")
@Tag(name = "02-车辆管理模块")
@Slf4j
@Validated
public class VehicleController {
    @Autowired
    VehicleMapper vehicleMapper;

    @PostMapping("add")
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleParam vehicleParam, HttpSession session) {
        log.debug("添加车辆参数："+vehicleParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if(userVO==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleParam,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());
        log.debug("添加车辆："+vehicle);
        int rows=vehicleMapper.insertVehicle(vehicle);
        if(rows>0) {
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @Operation(summary = "查询车辆列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("list")
    @Parameters(value = {
            @Parameter(name = "brand"),
            @Parameter(name = "license"),
            @Parameter(name = "vehicleListQuery",hidden = true)
    })
    public JsonResult selectVehicleList(VehicleListQuery vehicleListQuery,HttpSession session){
        log.debug("根据车牌查询的信息："+vehicleListQuery);
        UserVO userVO= (UserVO) session.getAttribute("user");
//        if(userVO==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        List<VehicleListVO> list = vehicleMapper.selectVehicleList(vehicleListQuery,userVO.getId());
        return JsonResult.ok(list);
    }

    @PostMapping("del/{id}")
    @Operation(summary = "删除车辆信息")
    @ApiOperationSupport(order = 30)
    public JsonResult delVehicle(@Range(min=1L,max=500L,message = "车牌编号id范围1-500之间") @PathVariable Long id, HttpSession session){
        log.debug("删除车辆信息："+id);
        //手动抛出异常，演示全局异常处理
        if (id<0) {
            throw new IllegalArgumentException("参数id不能小于0");
        }
        UserVO userVO= (UserVO) session.getAttribute("user");
//        if(userVO==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        int row=vehicleMapper.updateVehicle(id, userVO.getId());
        if(row!=1){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }return JsonResult.ok();
    }
}
